package verificateur;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;

import structureDonnee.Variable;

class VariableSearcher{
	//lire un ficher et distiguer des variables dans la premiere ligne du ficher
	private int carLu;
	private final StringBuilder buffer = new StringBuilder();
	private final BufferedReader b;
	 
	public VariableSearcher(String nom) {
		try {
			b = new BufferedReader(new InputStreamReader(new FileInputStream(nom),"UTF-8"));
		}catch (IOException e){
			throw new IllegalArgumentException("fichier illisible",e);	
		}
	} 
	 
	public LinkedList<Variable> litFichier() {
		try {
			LinkedList<Variable> ensemble = new LinkedList<Variable>();
			
			do{carLu = b.read();} while(carLu!=':'); //sauter la partie "var:" carLu==:
			do{
				carLu=b.read();
				String vn = readVariable();//distinguer un variable
				ensemble.add(new Variable(vn));//l'ajouter dans ensemble variable
			}while(carLu!='.');
	
			b.close();
			return ensemble;
		}	
		catch (IOException e) {
			try {
				b.close();
			} catch (IOException e1) {/* nothing */}	
			throw new IllegalArgumentException("invalid file",e);
		}
	}	
	 
	//distinguer variables et lire leurs noms 
	private String readVariable()throws IOException{
		buffer.setLength(0);
		while(','!=carLu&&'.'!=carLu){
			buffer.append((char)carLu);
			carLu=b.read();
		}	
		return buffer.toString();
	}	
}
